package com.lex.common.factory;

import com.lex.common.Constants;
import com.lex.common.utils.AddressUtils;
import com.lex.common.utils.IpUtil;
import com.lex.common.utils.SpringUtil;
import com.lex.data.admin.model.LexSysLog;
import com.lex.data.admin.model.LexSysLoginLog;
import com.lex.data.admin.service.MyLexSysLoginService;
import com.lex.data.admin.service.MySysLogService;
import eu.bitwalker.useragentutils.UserAgent;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.TimerTask;


public class AsyncFactory
{


    private static HttpServletRequest getRequest(){
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) attributes;
        return sra.getRequest();
    }

    /**
     * 记录登录信息
     *
     * @param username 用户名
     * @param status 状态
     * @param message 消息
     * @param args 列表
     * @return 任务task
     */
    public static TimerTask insertLoginLog(final String username, final String status, final String message,
                                             final Object... args)
    {
        String Header = getRequest().getHeader("User-Agent");
        final UserAgent userAgent = UserAgent.parseUserAgentString(Header);
        final String ip = IpUtil.getIpAddr(getRequest());
        final String address = AddressUtils.getRealAddressByIP(ip);
        return new TimerTask()
        {
            @Override
            public void run()
            {
                // 获取客户端操作系统
                String os = userAgent.getOperatingSystem().getName();
                // 获取客户端浏览器
                String browser = userAgent.getBrowser().getName();
                // 封装对象
                LexSysLoginLog login_log = new LexSysLoginLog();
                login_log.setLoginLocation(address);
                login_log.setUserName(username);
                login_log.setIpaddr(ip);
                login_log.setBrowser(browser);
                login_log.setOs(os);
                login_log.setMsg(message);
                // 日志状态
                if (Constants.LOGIN_SUCCESS.equals(status) || Constants.LOGOUT.equals(status) || Constants.REGISTER.equals(status))
                {
                    login_log.setStatus(Constants.SUCCESS);
                }
                else if (Constants.LOGIN_FAIL.equals(status))
                {
                    login_log.setStatus(Constants.FAIL);
                }
                // 插入数据
                SpringUtil.getBean(MyLexSysLoginService.class).inserLoginInfo(login_log);
            }
        };
    }

    /**
     * 操作日志记录
     *
     * @param Log 操作日志信息
     * @return 任务task
     */
    public static TimerTask OperationLog(final LexSysLog Log)
    {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                SpringUtil.getBean(MySysLogService.class).insertOperlog(Log);
            }
        };
    }
}